package oving8;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

public class ShapePanel extends SimplePanel{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private ArrayList<ShapeObject> shapeObjects= new ArrayList<ShapeObject>();
	private MoveableObject selectedObject;
	protected void paintPanel(Graphics2D g){
		for(int i=0;i<shapeObjects.size();i++){
			shapeObjects.get(i).paint(g);
		}
	}
	public void addShape(ShapeObject so){
		shapeObjects.add(so);
	}
	public void removeShape(ShapeObject so){
		if(shapeObjects.contains(so)){
			shapeObjects.remove(so);
		}
	}
	public MoveableObject getSelection(){
		return selectedObject;
	}
	protected void mouseClick(MouseEvent me){		
		boolean trykk=false;
		for(int i=0;i<shapeObjects.size();i++){
			if(shapeObjects.get(i) instanceof MoveableObject){				
				if(shapeObjects.get(i).contains(me.getX(),me.getY())){
					if(selectedObject!=null){
					selectedObject.setOutlineColor(Color.RED);
					selectedObject.setFillColor(Color.BLUE);
					}
					selectedObject=(MoveableObject)shapeObjects.get(i);					
					selectedObject.setOutlineColor(Color.PINK);
					selectedObject.setFillColor(Color.CYAN);
					trykk=true;
					break;
				}
			}
		}
		if(!trykk){
			selectedObject=null;
		}
	}
	protected void keyPress(KeyEvent e){
		if(selectedObject!=null){
			if(e.getKeyCode()==KeyEvent.VK_LEFT){
				selectedObject.move(-10, 0);
			}
			if(e.getKeyCode()==KeyEvent.VK_RIGHT){
				selectedObject.move(10, 0);
			}
			if(e.getKeyCode()==KeyEvent.VK_UP){
				selectedObject.move(0, -10);
			}
			if(e.getKeyCode()==KeyEvent.VK_DOWN){
				selectedObject.move(0, 10);
			}
		}
	}
	protected void tick(){
		for(int i=0;i<shapeObjects.size();i++){
			if(shapeObjects.get(i) instanceof MovingObject){
				((MovingObject)shapeObjects.get(i)).move();
			}
		}
	}
	public static void main(String[] args) {
		ShapePanel sp = new ShapePanel();
		sp.addShape(new MoveableObject(new Rectangle(100,100)));
		sp.addShape(new MoveableObject(new Rectangle(50,50)));
		sp.addShape(new MoveableObject(new Rectangle(200,200)));
		SimplePanel.createFrame(sp);
	}
}
